package org.easymis.air.southwest.modules.organize.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.easymis.air.southwest.modules.organize.service.OrganizeFunctionMng;
import org.easymis.entitys.mybatis.dto.OrganizeFunction;
import org.easymis.entitys.mybatis.mapper.OrganizeFunctionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrganizeFunctionMngImpl implements OrganizeFunctionMng {
	@Autowired
	private OrganizeFunctionMapper mapper;

	public List<OrganizeFunction> getList(OrganizeFunction bean) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("orgId", bean.getOrgId());
		params.put("level", bean.getLevel());
		params.put("parentId", bean.getParentId());
		params.put("staffId", bean.getStaffId());
		return this.mapper.getList(params);
	}
	
	public List<OrganizeFunction> getListByStaff(OrganizeFunction bean) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("orgId", bean.getOrgId());
		params.put("level", bean.getLevel());
		params.put("parentId", bean.getParentId());
		params.put("staffId", bean.getStaffId());
		return this.mapper.getListByStaff(params);
	}
	
	public List<OrganizeFunction> getBreadcrumb(String functionId) {
		List<OrganizeFunction> breadcrumbList = new ArrayList<OrganizeFunction>();
		OrganizeFunction current = this.mapper.findById(functionId);
		OrganizeFunction level3 = null;
		OrganizeFunction level2 = null;
		OrganizeFunction level1 = null;
		if (current.getLevel() == 4) {
			level3 = this.mapper.findById(current.getParentId());
			level2 = this.mapper.findById(level3.getParentId());
			level1 = this.mapper.findById(level2.getParentId());
			breadcrumbList.add(level1);
			breadcrumbList.add(level2);
			breadcrumbList.add(level3);
			breadcrumbList.add(current);
		} else if (current.getLevel() == 3) {
			level2 = this.mapper.findById(current.getParentId());
			level1 = this.mapper.findById(level2.getParentId());
			breadcrumbList.add(level1);
			breadcrumbList.add(level2);
			breadcrumbList.add(current);
		} else if (current.getLevel() == 2) {
			level1 = this.mapper.findById(current.getParentId());
			breadcrumbList.add(level1);
			breadcrumbList.add(current);
		} else {
			breadcrumbList.add(current);

		}

		return breadcrumbList;
	}
	public void save(OrganizeFunction bean) {
		this.mapper.save(bean);
	}

	public void update(OrganizeFunction bean) {
		this.mapper.update(bean);
	}

	public void delete(String functionId) {
		this.mapper.delete(functionId);
	}

	public OrganizeFunction findById(String functionId) {
		return this.mapper.findById(functionId);
	}

}
